package com.yami.shop.service.impl;

import cn.hutool.core.math.MathUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yami.shop.bean.enums.ProfitBackStatus;
import com.yami.shop.bean.enums.ProfitTypeConst;
import com.yami.shop.bean.model.*;
import com.yami.shop.common.util.Arith;
import com.yami.shop.dao.ProfitBackInfoMapper;
import com.yami.shop.service.ProductService;
import com.yami.shop.service.ProfitBackInfoService;
import com.yami.shop.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.List;
@Service
public class ProfitBackInfoServiceImpl extends ServiceImpl<ProfitBackInfoMapper, ProfitBackInfo> implements ProfitBackInfoService {
    @Autowired
    private ProfitBackInfoMapper profitBackInfoMapper;
    @Autowired
    private ProductService productService;

    @Autowired
    private UserService userService;

    @Override
    public double calProfitByType(Order order, ProfitTypeConst profitType) {
        List<OrderItem> orderItems = order.getOrderItems();
        double v = 0;
        for (OrderItem orderItem : orderItems) {
            Long prodId = orderItem.getProdId();
            Product product = productService.getById(prodId);
            Double productTotalAmount = orderItem.getProductTotalAmount();

            switch (profitType) {
                case SELF_BUY_PROFIT:
                    v += productTotalAmount * product.getSelfBuyProfit() / 100;
                    break;
                case SHARE_PORFIT:
                    v += productTotalAmount * product.getShareProfit() / 100;
                    break;
                case UPPER_PROFIT:
                    v += productTotalAmount * product.getUpProfit() / 100;
                    break;
            }
        }
        return Arith.round(v, 2);
    }

    @Override
    public List<ProfitBackInfo> getOneProfitBackInfos(String uid, Integer profitBackStatus) {
        List<ProfitBackInfo> list = list(Wrappers.lambdaQuery(new ProfitBackInfo())
                .eq(ProfitBackInfo::getToUserId, uid)
                .eq(profitBackStatus != null, ProfitBackInfo::getProfitStatus, profitBackStatus));
        return list;
    }
}
